package com.yyjj.yyjj.acl.controller;

import com.yyjj.yyjj.common.utils.MD5;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yyjj.yyjj.acl.service.AdminService;
import com.yyjj.yyjj.acl.service.RoleService;
import com.yyjj.yyjj.common.result.Result;
import com.yyjj.yyjj.model.acl.Admin;
import com.yyjj.yyjj.vo.acl.AdminQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/acl/user")
@RequiredArgsConstructor
@Api(tags = "用户管理")

public class AdminController {

    private final AdminService adminService;

    private final RoleService roleService;

    //用户列表
    @ApiOperation("用户列表")
    @GetMapping("/{current}/{limit}")
    public Result list(@PathVariable("current") Long current, @PathVariable("limit") Long limit, AdminQueryVo adminQueryVo) {
        Page<Admin> adminPage = new Page<>(current, limit);
        IPage<Admin> page = adminService.selectPageUser(adminPage, adminQueryVo);
        return Result.ok(page);
    }

    //根据id查询
    @ApiOperation("根据id查询")
    @GetMapping("/get/{id}")
    public Result getById(@PathVariable("id") Long id) {
        Admin admin = adminService.getById(id);
        return Result.ok(admin);
    }

    //添加用户
    @ApiOperation("添加用户")
    @PostMapping("/save")
    public Result save(@RequestBody Admin admin) {
        admin.setPassword(MD5.encrypt(admin.getPassword()));
        adminService.save(admin);
        return Result.ok(null);
    }

    //更新用户信息
    @ApiOperation("更新用户")
    @PutMapping("/update")
    public Result update(@RequestBody Admin admin) {
        adminService.updateById(admin);
        return Result.ok(null);
    }

    //根据id删除
    @ApiOperation("根据id删除用户")
    @DeleteMapping("/remove/{id}")
    public Result removeById(@PathVariable("id") Long id) {
        adminService.removeById(id);
        return Result.ok(null);
    }

    //批量删除
    @ApiOperation("批量删除")
    @DeleteMapping("/batchRemove")
    public Result batchRemove(@RequestBody List<Long> idList) {
        adminService.removeByIds(idList);
        return Result.ok(null);
    }

    //为用户分配角色

    //1.获取用户的所有角色
    @ApiOperation("分配角色:获取用户的所有角色")
    @GetMapping("/toAssign/{adminId}")
    public Result toAssign(@PathVariable("adminId") Long adminId) {
        Map<String,Object> map = roleService.getRoleByAdminId(adminId);
        return Result.ok(map);
    }


    //2.重新分配
    @ApiOperation("分配角色:重新分配")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestParam Long adminId,@RequestParam Long[] roleId){
        roleService.saveAdminRole(adminId,roleId);
        return Result.ok(null);

    }



}
